package edu.hubu.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.hubu.model.Users;
import edu.hubu.service.UserService;
import edu.hubu.service.impl.UserServiceImpl;

//@SuppressWarnings("all")
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");
        PrintWriter out = res.getWriter();

        String name = req.getParameter("username");
        String pwd = req.getParameter("password");

		System.out.println("name=" + name);
		System.out.println("pwd=" + pwd);

        //验证
        Users users = userService.getUserByName(name, pwd);
        System.out.println(users);

//		if ("张三".equals(name)&&"123".equals(pwd)) {
        if (users != null) {
            //succ
            req.getRequestDispatcher("/Main").forward(req, res);//请求转发
        } else {
            //err
            String errMsg = "用户不存在或密码错误，请重新输入！";
            req.setAttribute("errMsg", errMsg);
            req.getRequestDispatcher("/Login").forward(req, res);//请求转发
        }

    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {

        this.doGet(req, res);

    }

}
